<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="mini"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="principalName" label-width="42px">
        <el-input
          v-model.trim="queryParams.principalName"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="principalPhone" label-width="56px">
        <el-input
          v-model.trim="queryParams.principalPhone"
          placeholder="请输入负责人手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机类型">
        <el-select v-model="queryParams.userProperty">
          <el-option :value="1" label="员工"></el-option>
          <el-option :value="2" label="非员工"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" label-width="42px">
        <el-select v-model="queryParams.status">
          <el-option :value="0" label="已启用"></el-option>
          <el-option :value="1" label="已停用"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
         <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    </el-row>

    <el-table
      v-loading="loading"
      :data="infoList"
      max-height="390"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="姓名" min-width="120" align="center" prop="principalName"  :show-overflow-tooltip="true" />
      <el-table-column
        label="手机号"
        align="center"
        prop="principalPhone"
        min-width="120"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="司机类型" align="center" prop="userProperty">
        <template slot-scope="scope">
          {{ scope.row.userProperty === "1" ? "员工" : "非员工" }}
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age" min-width="80"/>
      <el-table-column label="身份证号" align="center" prop="idNumber" min-width="180"  :show-overflow-tooltip="true"/>
      <el-table-column label="驾龄" align="center" prop="drivingYears" />
      <el-table-column
        label="驾照类型"
        align="center"
        prop="drivingLicenseType"
        min-width="80"
      >
        <template slot-scope="scope">
          <span>{{ driverEnum[scope.row.drivingLicenseType] }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="有效期"
        align="center"
        prop="expireTime"
        min-width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" min-width="120" show-overflow-tooltip/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            disabled
            inactive-value="1"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="220px"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            :disabled="!scope.row.userId"
            @click="handleResetPwd(scope.row)"
            >重置密码</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAccount(scope.row)"
            >账号管理</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改用户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="460px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="82px">
        <el-form-item label="姓名" prop="principalName">
          <el-input
            disabled
            v-model="form.principalName"
            placeholder="请输入负责人"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="principalPhone">
          <el-input
            v-model="form.principalPhone"
            placeholder="请输入负责人手机号"
          />
        </el-form-item>
        <el-form-item label="司机类型">
          <el-radio-group v-model="form.userProperty" disabled>
            <el-radio label="1">员工</el-radio>
            <el-radio label="2">非员工</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="账号" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item
          label="账号有效期"
          prop="expireTime"
          v-if="form.userProperty === '2'"
        >
          <el-date-picker
            type="date"
            value-format="yyyy-MM-dd"
            v-model="form.expireTime"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="账号状态">
          <el-switch
            v-model="form.status"
            active-value="0"
            inactive-value="1"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo,
  updateAccountInfo
} from "@/api/system/info";
// import {
//   listUser,
//   getUser,
//   delUser,
//   addUser,
//   updateUser,
//   resetUserPwd,
//   changeUserStatus,
//   deptTreeSelect,
// } from "@/api/system/user";
import { resetUserPwd, listUser } from "@/api/system/user";
import { validatePhoneNumber } from "@/utils/validator";
const driverEnum = {
  1: "A1",
  2: "A2",
  3: "A3",
  4: "B1",
  5: "B2",
  6: "C1",
  7: "C2",
  8: "C3",
};
export default {
  name: "Info",
  data() {
    return {
      driverEnum,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userType: 2,
        principalName: null,
        principalPhone: null,
        expireTime: null,
        status: null,
        userProperty: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "账号是必填项", trigger: "blur,change" },
        ],
        principalPhone: validatePhoneNumber(),
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleUpdate(row) {
      this.$router.push({
        path: `/logistics/driverAction?id=${row.userInfoId}`,
      });
    },
    /** 重置密码按钮操作 */
    handleResetPwd(row) {
      this.$confirm("重置后，默认密码为123123", "重置密码", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
      })
        .then(() => {
          resetUserPwd(row.userId, "123123").then((response) => {
            this.$modal.msgSuccess("修改成功，新密码是：" + 123123);
          });
        })
        .catch(() => {});
    },
    /** 查询用户信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then((response) => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userProperty: null,
        subjectName: null,
        idNumber: null,
        principalName: null,
        principalPhone: null,
        status: null,
        userName: null,
        expireTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        userType: 2,
        principalName: null,
        principalPhone: null,
        expireTime: null,
        status: null,
        userProperty: null,
      };
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userInfoId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        path: "/logistics/driverAction",
      });
      //   this.reset();
      //   this.open = true;
      //   this.title = "添加用户信息";
    },
    /** 修改按钮操作 */
    handleAccount(row) {
      this.reset();
      this.open = true;
      this.title = "账号管理";
      this.form = { ...this.form, ...row };
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.userName != null) {
            updateAccountInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userInfoIds = row.userInfoId || this.ids;
      this.$modal
        .confirm('是否确认删除用户信息编号为"' + userInfoIds + '"的数据项？')
        .then(function () {
          return delInfo(userInfoIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/info/export",
        {
          ...this.queryParams,
        },
        `info_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
